home *** CD-ROM | disk | FTP | other *** search
/ Isometric Game Programming with DirectX 7.0 / Isometric Game Programming.iso / source / chapter3 / isohex3_8 / isohex3_8.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-19  |  8.2 KB  |  312 lines

  1. /*****************************************************************************
  2. IsoHex3_8.cpp
  3. Ernest S. Pazera
  4. 18MAY2000
  5. Start a WIN32 Application Workspace, add in this file
  6. Requires GDICanvas.h and GDICanvas.cpp
  7. *****************************************************************************/
  8.  
  9. //////////////////////////////////////////////////////////////////////////////
  10. //INCLUDES
  11. //////////////////////////////////////////////////////////////////////////////
  12. #define WIN32_LEAN_AND_MEAN  
  13.  
  14. #include <windows.h>   
  15. #include "GDICanvas.h"
  16.  
  17. //////////////////////////////////////////////////////////////////////////////
  18. //DEFINES
  19. //////////////////////////////////////////////////////////////////////////////
  20. //name for our window class
  21. #define WINDOWCLASS "ISOHEX3"
  22. //title of the application
  23. #define WINDOWTITLE "IsoHex 3-8"
  24.  
  25. //////////////////////////////////////////////////////////////////////////////
  26. //PROTOTYPES
  27. //////////////////////////////////////////////////////////////////////////////
  28. bool Prog_Init();//game data initalizer
  29. void Prog_Loop();//main game loop
  30. void Prog_Done();//game clean up
  31. //functions to deal with the update rectangle
  32. void ClearUpdate();//clears the update rectangle
  33. void AddUpdate(RECT* prcAdd);//adds the update rectangle
  34. void RenderUpdate(HWND hwndDst, HDC hdcSrc);//renders the update
  35.  
  36. //////////////////////////////////////////////////////////////////////////////
  37. //GLOBALS
  38. //////////////////////////////////////////////////////////////////////////////
  39. HINSTANCE hInstMain=NULL;//main application handle
  40. HWND hWndMain=NULL;//handle to our main window
  41. //gdi canvases
  42. CGDICanvas gdicTile;
  43. CGDICanvas gdicMask;
  44. CGDICanvas gdicBackbuffer;
  45. //update rectangle
  46. RECT rcUpdate;
  47.  
  48. //////////////////////////////////////////////////////////////////////////////
  49. //WINDOWPROC
  50. //////////////////////////////////////////////////////////////////////////////
  51. LRESULT CALLBACK TheWindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
  52. {
  53.     //which message did we get?
  54.     switch(uMsg)
  55.     {
  56.     case WM_LBUTTONDOWN:
  57.         {
  58.             //borrow dc from main window
  59.             HDC hdc=GetDC(hWndMain);
  60.  
  61.             //blit from the memory dc to the window's dc
  62.             BitBlt(gdicBackbuffer,LOWORD(lParam)-gdicMask.GetWidth()/2,HIWORD(lParam)-gdicMask.GetHeight()/2,gdicMask.GetWidth(),gdicMask.GetHeight(),gdicMask,0,0,SRCAND);
  63.             BitBlt(gdicBackbuffer,LOWORD(lParam)-gdicTile.GetWidth()/2,HIWORD(lParam)-gdicTile.GetHeight()/2,gdicMask.GetWidth(),gdicMask.GetHeight(),gdicTile,0,0,SRCPAINT);
  64.  
  65.             //add to the update rectangle
  66.             RECT rcAdd;
  67.             SetRect(&rcAdd,LOWORD(lParam)-gdicMask.GetWidth()/2,HIWORD(lParam)-gdicMask.GetHeight()/2,LOWORD(lParam)-gdicMask.GetWidth()/2+gdicMask.GetWidth(),HIWORD(lParam)-gdicMask.GetHeight()/2+gdicMask.GetHeight());
  68.             AddUpdate(&rcAdd);
  69.             
  70.             //return the borrowed dc to the system
  71.             ReleaseDC(hWndMain,hdc);
  72.  
  73.             //handled, so return 0
  74.             return(0);
  75.         }break;
  76.     case WM_DESTROY://the window is being destroyed
  77.         {
  78.  
  79.             //tell the application we are quitting
  80.             PostQuitMessage(0);
  81.  
  82.             //handled message, so return 0
  83.             return(0);
  84.  
  85.         }break;
  86.     case WM_PAINT://the window needs repainting
  87.         {
  88.             //a variable needed for painting information
  89.             PAINTSTRUCT ps;
  90.             
  91.             //start painting
  92.             HDC hdc=BeginPaint(hwnd,&ps);
  93.  
  94.             /////////////////////////////
  95.             //painting code would go here
  96.             /////////////////////////////
  97.  
  98.             //end painting
  99.             EndPaint(hwnd,&ps);
  100.  
  101.             //add entire client are to update rectangle
  102.             RECT rcClient;
  103.             GetClientRect(hwnd,&rcClient);
  104.             AddUpdate(&rcClient);
  105.                         
  106.             //handled message, so return 0
  107.             return(0);
  108.         }break;
  109.     }
  110.  
  111.     //pass along any other message to default message handler
  112.     return(DefWindowProc(hwnd,uMsg,wParam,lParam));
  113. }
  114.  
  115.  
  116. //////////////////////////////////////////////////////////////////////////////
  117. //WINMAIN
  118. //////////////////////////////////////////////////////////////////////////////
  119. int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
  120. {
  121.     //assign instance to global variable
  122.     hInstMain=hInstance;
  123.  
  124.     //create window class
  125.     WNDCLASSEX wcx;
  126.  
  127.     //set the size of the structure
  128.     wcx.cbSize=sizeof(WNDCLASSEX);
  129.  
  130.     //class style
  131.     wcx.style=CS_OWNDC | CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
  132.  
  133.     //window procedure
  134.     wcx.lpfnWndProc=TheWindowProc;
  135.  
  136.     //class extra
  137.     wcx.cbClsExtra=0;
  138.  
  139.     //window extra
  140.     wcx.cbWndExtra=0;
  141.  
  142.     //application handle
  143.     wcx.hInstance=hInstMain;
  144.  
  145.     //icon
  146.     wcx.hIcon=LoadIcon(NULL,IDI_APPLICATION);
  147.  
  148.     //cursor
  149.     wcx.hCursor=LoadCursor(NULL,IDC_ARROW);
  150.  
  151.     //background color
  152.     wcx.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
  153.  
  154.     //menu
  155.     wcx.lpszMenuName=NULL;
  156.  
  157.     //class name
  158.     wcx.lpszClassName=WINDOWCLASS;
  159.  
  160.     //small icon
  161.     wcx.hIconSm=NULL;
  162.  
  163.     //register the window class, return 0 if not successful
  164.     if(!RegisterClassEx(&wcx)) return(0);
  165.  
  166.     //create main window
  167.     hWndMain=CreateWindowEx(0,WINDOWCLASS,WINDOWTITLE, WS_BORDER | WS_SYSMENU | WS_VISIBLE,0,0,320,240,NULL,NULL,hInstMain,NULL);
  168.  
  169.     //error check
  170.     if(!hWndMain) return(0);
  171.  
  172.     //if program initialization failed, then return with 0
  173.     if(!Prog_Init()) return(0);
  174.  
  175.     //message structure
  176.     MSG msg;
  177.  
  178.     //message pump
  179.     for(;;)    
  180.     {
  181.         //look for a message
  182.         if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  183.         {
  184.             //there is a message
  185.  
  186.             //check that we arent quitting
  187.             if(msg.message==WM_QUIT) break;
  188.             
  189.             //translate message
  190.             TranslateMessage(&msg);
  191.  
  192.             //dispatch message
  193.             DispatchMessage(&msg);
  194.         }
  195.  
  196.         //run main game loop
  197.         Prog_Loop();
  198.     }
  199.     
  200.     //clean up program data
  201.     Prog_Done();
  202.  
  203.     //return the wparam from the WM_QUIT message
  204.     return(msg.wParam);
  205. }
  206.  
  207. //////////////////////////////////////////////////////////////////////////////
  208. //INITIALIZATION
  209. //////////////////////////////////////////////////////////////////////////////
  210. bool Prog_Init()
  211. {
  212.     //borrow dc from main window
  213.     HDC hdc=GetDC(hWndMain);
  214.  
  215.     //load the images
  216.     gdicTile.Load(hdc,"IsoHex3_8-1.bmp");
  217.     gdicMask.Load(hdc,"IsoHex3_8-2.bmp");
  218.  
  219.     //get the client rectangle
  220.     RECT rcClient;
  221.     GetClientRect(hWndMain,&rcClient);
  222.  
  223.     //create a blank bitmap with the client area's dimensions
  224.     gdicBackbuffer.CreateBlank(hdc,rcClient.right,rcClient.bottom);
  225.  
  226.     //clear out the blank bitmap
  227.     FillRect(gdicBackbuffer,&rcClient,(HBRUSH)GetStockObject(BLACK_BRUSH));
  228.  
  229.     //clear the update region
  230.     ClearUpdate();
  231.  
  232.     //return dc to system
  233.     ReleaseDC(hWndMain,hdc);
  234.  
  235.     return(true);//return success
  236. }
  237.  
  238. //////////////////////////////////////////////////////////////////////////////
  239. //CLEANUP
  240. //////////////////////////////////////////////////////////////////////////////
  241. void Prog_Done()
  242. {
  243.     //destroy the images
  244.     gdicTile.Destroy();
  245.     gdicMask.Destroy();
  246. }
  247.  
  248. //////////////////////////////////////////////////////////////////////////////
  249. //MAIN GAME LOOP
  250. //////////////////////////////////////////////////////////////////////////////
  251. void Prog_Loop()
  252. {
  253.     //render the update
  254.     RenderUpdate(hWndMain,gdicBackbuffer);
  255. }
  256.  
  257. //clears the update rectangle
  258. void ClearUpdate()
  259. {
  260.     //set the update rect to empty
  261.     SetRectEmpty(&rcUpdate);
  262. }
  263.  
  264. //adds the update rectangle
  265. void AddUpdate(RECT* prcAdd)
  266. {
  267.     //if the new rectangle is empty, return without doing anything
  268.     if(IsRectEmpty(prcAdd)) return;
  269.  
  270.     if(IsRectEmpty(&rcUpdate))
  271.     {
  272.         //if the rectangle is empty
  273.  
  274.         //copy the new rectangle to the update rectangle
  275.         CopyRect(&rcUpdate,prcAdd);
  276.  
  277.     }
  278.     else
  279.     {
  280.         //if the rectangle is not empty
  281.  
  282.         //create a temporary rectangle
  283.         RECT rcTemp;
  284.  
  285.         //combine the new rectangle with the old rectangle in the temporary rect
  286.         UnionRect(&rcTemp,&rcUpdate,prcAdd);
  287.  
  288.         //copy the temporary rectangle to the update rect
  289.         CopyRect(&rcUpdate,&rcTemp);
  290.     }
  291. }
  292.  
  293. //renders the update
  294. void RenderUpdate(HWND hwndDst, HDC hdcSrc)
  295. {
  296.     //if the update rectangle is empty, return without doing anything
  297.     if(IsRectEmpty(&rcUpdate)) return;
  298.  
  299.     //borrow the dc from the destination window
  300.     HDC hdcDst=GetDC(hwndDst);
  301.  
  302.     //blit the update area
  303.     BitBlt(hdcDst,rcUpdate.left,rcUpdate.top,rcUpdate.right-rcUpdate.left,rcUpdate.bottom-rcUpdate.top,hdcSrc,rcUpdate.left,rcUpdate.top,SRCCOPY);
  304.  
  305.     //return the destination dc to the system
  306.     ReleaseDC(hwndDst,hdcDst);
  307.  
  308.     //clear the update area
  309.     ClearUpdate();
  310. }
  311.  
  312.